﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using XMK.Tadong.Model;
using XMK.Tadong.Data;
using XMK.Utils;
using XMK.Framework.Model;

namespace XMK.Tadong.Biz
{
    public class XMKLogBiz
    {
        public static void WriteLog(XMKLogInfo info)
        {
            XMKLogData data = new XMKLogData(Config.TadongDbProvider);
            try
            {
                data.WriteLog(info);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(Utils.LogType.Error, "记录日志异常,"+ ex.ToString());
            }
        }
        /// <summary>
        /// 添加文件操作日志
        /// </summary>
        /// <param name="operateType">操作类型，LogOperateType.Search、ScanFile、DownFile、DeleteFile</param>
        /// <param name="fileName">文件名</param>
        /// <param name="fileId">文件id</param>
        /// <param name="spaceType">空间类型，"20"-课题组、"30"-项目库</param>
        /// <param name="ip">ip地址</param>
        public static void WriteLogFile(LogOperateType operateType, string fileName,long fileId, string spaceType, string ip)
        {
            string operateDesc=" ";
            if (operateType == LogOperateType.Search)
                operateDesc = "Sserch";
            else if (operateType == LogOperateType.ScanFile)
                operateDesc = "Online browsing";
            else if (operateType == LogOperateType.DownFile)
                operateDesc = "Download";
            else if (operateType == LogOperateType.DeleteFile)
                operateDesc = "Delete";

            string spaceName = spaceType.Equals("20") ? "WorkGroup" : "Project";
            XMKLogInfo logModel = new XMKLogInfo();
            logModel.DataId = fileId;
            logModel.Keyword = fileName;
            logModel.LogDesc = string.Format("【{0}】【{1}】{3} {4}file,{2}",
                LoginUserBiz.LoginUserName(), LoginUserBiz.LoginUserAccount(), fileName, operateDesc, spaceName);
            logModel.OperateTime = DateTime.Now;
            logModel.OperateType = operateType;
            logModel.UserId = LoginUserBiz.LoginUserID();
            logModel.IP = ip;
            WriteLog(logModel);
        }
        /// <summary>
        /// 浏览页面
        /// </summary>
        /// <param name="pageName">页面名称</param>
        public static void WriteLogScanPage(XMK.Tadong.Model.Cond.Base_PageLog pageLogModel )
        {
            XMKLogInfo logModel = new XMKLogInfo();
            logModel.Keyword = "browsing Page";
            logModel.LogDesc = string.Format("【{0}】【{1}】{2} {3}",
            LoginUserBiz.LoginUserName(), LoginUserBiz.LoginUserAccount(), "browsing", pageLogModel.pageName);
            logModel.OperateTime = DateTime.Now;
            logModel.OperateType = LogOperateType.ScanPage;
            logModel.UserId = LoginUserBiz.LoginUserID();
            logModel.IP = pageLogModel.ip;
            XMKLogBiz.WriteLog(logModel);
        }
        /// <summary>
        /// 导出结构化
        /// </summary>
        /// <param name="pageName">表名称</param>
        public static void WriteLogExcel(string tableName,string ip)
        {
            XMKLogInfo logModel = new XMKLogInfo();
            logModel.Keyword = "save table data";
            logModel.LogDesc = string.Format("【{0}】【{1}】{2} {3}",
            LoginUserBiz.LoginUserName(), LoginUserBiz.LoginUserAccount(), "save", tableName);
            logModel.OperateTime = DateTime.Now;
            logModel.OperateType = LogOperateType.DownFile;
            logModel.UserId = LoginUserBiz.LoginUserID();
            logModel.IP =ip;
            XMKLogBiz.WriteLog(logModel);
        }
        /// <summary>
        /// 日志查询
        /// </summary>
        /// <param name="page">分页信息</param>
        /// <param name="operType">操作类型枚举</param>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">截止日期</param>
        /// <param name="keyw">关键词</param>
        /// <returns></returns>
        public static ResponseInfo QueryLog(PageInfo page, LogOperateType operType,
            string userId, DateTime startDate, DateTime endDate, string keyw)
        {
            XMKLogData data = new XMKLogData(Config.TadongDbProvider);
            ResponseInfo res = null;
            
            try
            {
                res = new ResponseInfo();
                res.Data = data.QueryLog(page, operType, userId, startDate, endDate, keyw);
                res.Page = page;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(Utils.LogType.Error, "have anomaly" + ex.ToString());
            }
            return res;
        }

        /// <summary>
        /// 日志查询
        /// </summary>
        /// <param name="operType">操作类型枚举</param>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">截止日期</param>
        /// <param name="keyw">关键词</param>
        /// <returns></returns>
        public static ResponseInfo QueryLog(LogOperateType operType,
            string userId, DateTime startDate, DateTime endDate, string keyw)
        {
            XMKLogData data = new XMKLogData(Config.TadongDbProvider);
            ResponseInfo res = null;

            try
            {
                res = new ResponseInfo();
                res.Data = data.QueryLog(operType, userId, startDate, endDate, keyw);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(Utils.LogType.Error, "have anomaly" + ex.ToString());
            }
            return res;
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="operType">操作类型枚举</param>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">截止日期</param>
        /// <param name="keyw">关键词</param>
        /// <returns></returns>
        public static ResponseInfo DeleteLog(LogOperateType operType,
            string userId, DateTime startDate, DateTime endDate, string keyw)
        {
            XMKLogData data = new XMKLogData(Config.TadongDbProvider);
            ResponseInfo res = new ResponseInfo();

            try
            {
                res.Data = data.DeleteLog(operType, userId, startDate, endDate, keyw);
            }
            catch (Exception ex)
            {
                res.ErrMsg = ex.ToString();
                LogHelper.WriteLog(Utils.LogType.Error, "have anomaly" + ex.ToString());
            }
            return res;
        }
    }
}
